import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import qs from 'qs'
import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage
} from './common.js'

// 引用字体图标
import './assets/fonts/iconfont.css'

// 引用全局的样式
import './assets/css/global.css'

//导入axios
import axios from 'axios'
//配置请求根目录
axios.defaults.baseURL = 'http://localhost:8085/his_cms/'
Vue.prototype.$axios = axios

Vue.prototype.$qs = qs;

Vue.prototype.$getCurDate = getCurDate;
Vue.prototype.$setSessionStorage = setSessionStorage;
Vue.prototype.$getSessionStorage = getSessionStorage;
Vue.prototype.$removeSessionStorage = removeSessionStorage;
Vue.prototype.$setLocalStorage = setLocalStorage;
Vue.prototype.$getLocalStorage = getLocalStorage;
Vue.prototype.$removeLocalStorage = removeLocalStorage;

//请求到达服务器之前，先调用use中的回调函数来添加一个请求头信息(token)
axios.interceptors.request.use(config =>{
  // 从sessionStorage中获取到token然后给headers添加一个授权的字段Authorization
  config.headers.Authorization = sessionStorage.getItem('token');
  console.log(config);
  return config
})
Vue.config.productionTip = false

router.beforeEach(function(to,from,next){
	//除了登录之外都需要判断是否登录
	if(!(to.path=='/login')){
		let token = sessionStorage.getItem('token');
		if(token==null){
			router.push('/login');
			location.reload();
		}
	}
	if(to.path=='/rule'){
		let pass = sessionStorage.getItem('pass');
		if(pass!=1){
			router.push('/home')
			location.reload();
		}
	}
	next();
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')